package com.huwaiwai.thread_study.demo02;

/**
 * Created by hww on 2018/8/6 上午12:04.
 */
public class Test {

    public static void main(String [] args) {
        main01();
    }

    private static void main01() {
        long start = System.currentTimeMillis();
        long y;
        SleepInterrupt sl = new SleepInterrupt();
        Thread t = new Thread(sl);
        t.start();

        try {
            Thread.sleep(2000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("start interrupt" + (y = System.currentTimeMillis() - start));
        //interrupt 能直接中断线程
        t.interrupt();
        System.out.println("ending..." + (System.currentTimeMillis() - y -start));

    }

    static class SleepInterrupt extends Thread {
        @Override
        public void run() {
            long start = System.currentTimeMillis();
            try {
                System.out.println("start sleep...");
                Thread.sleep(20000L);
                System.out.println("work up");
            } catch (InterruptedException e) {
                System.out.println("catch interrupt" + (System.currentTimeMillis() - start));
                return;
            }
            System.out.println("not return");
        }
    }


}


